package com.api.customization.test.demo;

import com.alibaba.fastjson.JSONObject;
import com.customization.test.demo.service.WDetailsService;
import com.customization.test.demo.service.impl.WDetailsServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import com.customization.test.demo.util.FileUtils;
import com.engine.common.util.ServiceUtil;
import com.sun.jersey.multipart.FormDataParam;
import weaver.email.EmailWorkRunnable;
import weaver.general.StringUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.File;

/**
 * @author sjm
 * @description 清算资金明细清算请款
 */
@Path("/market")
public class ClearingTestDetailsController {

    private static final Logger log = LoggerFactory.getLogger(ClearingTestDetailsController.class);

    private WDetailsService getService(User user) {
        return ServiceUtil.getService(WDetailsServiceImpl.class, user);
    }

    @POST
    @Path("/clear/payment/test")
    @Produces("application/json")
    public String clearingPaymentTest(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "执行成功！");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);

            int testId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "test_id"));
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            String body = FormDataUtils.readBody(request);
            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);
                String ids = parseJson.getString("ids");
                if (ids != null && !ids.equals("")) {
                    String resultStr = getService(user).clearingTest(ids, user);

                    String subject = "《测试-zzz》";//邮件主题
                    String sendTo = "sjm864907017@163.com,864907017@qq.com";//邮件接收邮箱
                    String link = "<a href='http://localhost:8080/wui/index.html#/main?menuIds=-107,-109&menuPathIds=-107,-109&_key=b6f9iy'>《测试》</a>";
                    //String link = "<a href='"+host+"/spa/cube/index.html#/main/cube/card?type=0&modeId=32501&formId=-332&billid="+billid+"'>《"+htqcfwtx+"》</a>";
                    String sendText = link+" <br/> 测试邮件发送";//邮件正文
                    //组合邮件内容，发送附件
                    EmailWorkRunnable ewr = new EmailWorkRunnable(sendTo, subject, sendText);
                    boolean isPush = ewr.emailCommonRemind(); //result:true投递成功，false投递失败

                    if(resultStr != null){
                        result.put("msg",resultStr);
                        result.put("code", 500);
                        result.put("errorCode", 10003);
                    }
                } else {
                    result.put("msg", "请选择清算请款数据！");
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                }
            } else {
                result.put("msg", "请求体参数为空！");
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", e.getMessage());
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @POST
    @Path("/saveFileUpload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces({"application/json"})
    public JSONObject saveFileUpload(File uploadedFile,@Context HttpServletRequest request, @Context HttpServletResponse response) {
        log.info("uploadedFile");
        log.info(uploadedFile.length());
        JSONObject json = new JSONObject();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            String key = request.getParameter("name");
            FileUtils.uploadFile(uploadedFile,user,"uf_csbd","","id","");
            json.put("state", 1);
            json.put("success", true);
            return json;
        } catch (Exception var13) {
            json.put("n_a_s", 0);
            json.put("atts", (Object)null);
            json.put("success", false);
            return json;
        }
    }

}
